/**
 * 日期转时间戳
 * */ 

export function dateOrTime(time){
  var date = new Date(time.replace(/-/g, '/'));
  var t =  Date.parse(date);
  return t.toString().substring(0,10);
  // var date = new Date(time.replace(/-/g, '/'));
//   var date = new Date(time);
//  var t = date.getTime();
//   // var t =  Date.parse(date);
//   return t.toString().substring(0,10)
}

/**
 * 时间戳转为日期
 * **/ 
export function intervalTime(timestamp) {
  var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
  var Y = date.getFullYear() + '-';
  var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
  var D = (date.getDate() < 10 ? '0'+(date.getDate()) : date.getDate()) + ' ';
  var h = (date.getHours() < 10 ? '0'+(date.getHours()) : date.getHours()) + ':';
  var m = (date.getMinutes() < 10 ? '0'+(date.getMinutes()) : date.getMinutes()) + ':';
  var s = (date.getSeconds() < 10 ? '0'+(date.getSeconds()) : date.getSeconds());
  return Y+M+D+h+m+s;
}

/**
 *后端返回的图片下载
 * 
 * **/ 
export function downloadByBlob(url,name){
  let image = new Image()
  image.setAttribute('crossOrigin', 'anonymous')
  image.src = url
  image.onload = () => {
    let canvas = document.createElement('canvas')
    canvas.width = image.width
    canvas.height = image.height
    let ctx = canvas.getContext('2d')
    ctx.drawImage(image, 0, 0, image.width, image.height)
    canvas.toBlob((blob) => {
      let url = URL.createObjectURL(blob)
      download(url,name)
      // 用完释放URL对象
      URL.revokeObjectURL(url)
    })
  }
}
 function download(href, name) {
   let eleLink = document.createElement('a')
   eleLink.download = name
   eleLink.href = href
   eleLink.click()
   eleLink.remove()
 }
/**
 * 
 * **/ 
var now = new Date(); // 当前日期
var nowDayOfWeek = now.getDay(); // 今天本周的第几天
var nowDay = now.getDate(); // 当前日
var nowMonth = now.getMonth(); // 当前月
var nowYear = now.getYear(); // 当前年
nowYear += nowYear < 2000 ? 1900 : 0;

/**
 * @param 日期格式化
 * @returns {String}
 */
function formatDate(date) {
  var myyear = date.getFullYear();
  var mymonth = date.getMonth() + 1;
  var myweekday = date.getDate();
 
  if (mymonth < 10) {
    mymonth = "0" + mymonth;
  }
  if (myweekday < 10) {
    myweekday = "0" + myweekday;
  }
  return myyear + "-" + mymonth + "-" + myweekday;
}

/**
 * 获得本月的开始时间
 *
 */
export function getStartDayOfMonth() {
  var monthStartDate = new Date(nowYear, nowMonth, 1);
  return formatDate(monthStartDate);
}
 
/**
 * 获得本月的结束时间
 *
 */
export function getEndDayOfMonth() {
  var monthEndDate = new Date(nowYear, nowMonth, getMonthDays());
  return formatDate(monthEndDate);
}
 
/**
 * 获得本月天数
 *
 */
export function getMonthDays() {
  var monthStartDate = new Date(nowYear, nowMonth, 1);
  var monthEndDate = new Date(nowYear, nowMonth + 1, 1);
  var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
  return days;
}

export function checkQuarterTime () {


  // 获得本季度开始月份
  var quarterSeasonStartMonth = getQuarterSeasonStartMonth(nowMonth)
  // 获得本季度结束月份
  var quarterSeasonEndMonth = quarterSeasonStartMonth + 2
 
  // 获得本季度开始的日期
  var quarterSeasonStartDate = new Date(nowYear, quarterSeasonStartMonth, 1)
  // 获得本季度结束的日期
  var quarterSeasonEndDate = new Date(nowYear, quarterSeasonEndMonth, getMonthDays(nowYear, quarterSeasonEndMonth))
 
  var dcreatedateBegin = formatDate(quarterSeasonStartDate) // 本季度起始时间
  // 添加本周最后一天时间
  var dcreatedateEnd = formatDate(quarterSeasonEndDate) // 本季度终止时间
  return {start:dcreatedateBegin,end:dcreatedateEnd}
}

/**
 * 获取当前年份的第一天
 * @returns {string} 例如 2022-01-01
 */
export function getYearFirstDay() {
  var date = new Date();
  var year = date.getFullYear();
  return year + '-01-01'
}

/**
* 获取当前年份的最后一天
* @returns {string} 2022-12-31
*/
export function getYearLastDay(){
  var date = new Date();
  var year = date.getFullYear();
  return year + '-12-31'
}




export function getQuarterSeasonStartMonth (month) {
  // 得到本季度开始的月份
  var spring = 0 // 春
  var summer = 3 // 夏
  var fall = 6 // 秋
  var winter = 9 // 冬
  // 月份从0-11
  if (month < 3) {
    return spring
  }
  if (month < 6) {
    return summer
  }
  if (month < 9) {
    return fall
  }
  return winter
}


/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat
 * @returns {string | null}
 */
export function parseTime(time, cFormat) {
  if (arguments.length === 0 || !time) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string')) {
      if ((/^[0-9]+$/.test(time))) {
        // support "1548221490638"
        time = parseInt(time)
      } else {
        // support safari
        // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
        time = time.replace(new RegExp(/-/gm), '/')
      }
    }

    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
    return value.toString().padStart(2, '0')
  })
  return time_str
}

/**
 * @param {number} time
 * @param {string} option
 * @returns {string}
 */
export function formatTime(time, option) {
  if (('' + time).length === 10) {
    time = parseInt(time) * 1000
  } else {
    time = +time
  }
  const d = new Date(time)
  const now = Date.now()

  const diff = (now - d) / 1000

  if (diff < 30) {
    return '刚刚'
  } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + '分钟前'
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + '小时前'
  } else if (diff < 3600 * 24 * 2) {
    return '1天前'
  }
  if (option) {
    return parseTime(time, option)
  } else {
    return (
      d.getMonth() +
      1 +
      '月' +
      d.getDate() +
      '日' +
      d.getHours() +
      '时' +
      d.getMinutes() +
      '分'
    )
  }
}

/**
 * @param {string} url
 * @returns {Object}
 */
export function param2Obj(url) {
  const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
  if (!search) {
    return {}
  }
  const obj = {}
  const searchArr = search.split('&')
  searchArr.forEach(v => {
    const index = v.indexOf('=')
    if (index !== -1) {
      const name = v.substring(0, index)
      const val = v.substring(index + 1, v.length)
      obj[name] = val
    }
  })
  return obj
}
